package com.fendaren.api;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Slf4j
@Component
@Aspect
public class ServiceLogAspect {

    @Around("execution(* com.fendaren.service.impl..*.*(..))")
    public Object recordTimeLog(ProceedingJoinPoint joinPoint) throws Throwable {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        long begin = System.currentTimeMillis();
        Object proceed = joinPoint.proceed();
        String point = joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName();
        stopWatch.stop();
        long end = System.currentTimeMillis();
        long takeTime = end - begin;
        log.info(stopWatch.prettyPrint());
        if (takeTime > 1000) {
            log.error("执行方法: {} 执行时间太长了，耗费了{}毫秒", point, takeTime);
        } else if (takeTime > 2000) {
            log.error("执行方法: {} 执行时间稍微有点长，耗费了{}毫秒", point, takeTime);
        } else {
            log.info("执行方法: {} 执行时间,耗费了{}毫秒", point, takeTime);
        }
        return proceed;
    }
}
